/*
 * Created on 2005/4/3
 *
 */
package br.com.wai2008.client.presentation;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.ImageObserver;

import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import br.com.wai2008.client.constants.ClientConstants;

/**
 * @author Lin Yu Ching
 *  
 */
public class WAI2008ServiceBar extends Panel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6310475180371729668L;
	Applet app;
	WAI2008ServiceMap map;

	public WAI2008ServiceBar(Applet _app, WAI2008ServiceMap _mapa) {
		app = _app;
		map = _mapa;
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
//		Image imgFerramenta = app.getImage(app.getDocumentBase(),
//				"br/com/wai2008/client/presentation/barra_ferramenta.gif");
		add(this.buildCommandsBar());
		//add(new BarraCanvas(app, _mapa, imgFerramenta));
		app.add(BorderLayout.SOUTH, this.buildSimulationBar());
		setBounds(0, 0, WAI2008Interface.APP_W, 25);
	}
	
	private JPanel buildSimulationBar() {
		JPanel panel = new JPanel();
		
		//simulacao
		JLabel simulationLabel = new JLabel("Simulation:");
		JLabel pointLabel = new JLabel(" Point:");
		JLabel xLabel = new JLabel(" X:");
		JLabel yLabel = new JLabel(" Y:");
		JLabel floorLabel = new JLabel(" Floor:");
		
		panel.add(BorderLayout.EAST, simulationLabel);
		panel.add(BorderLayout.EAST, pointLabel);
		panel.add(BorderLayout.EAST, map.getPointText());
		panel.add(BorderLayout.EAST, xLabel);
		panel.add(BorderLayout.EAST, map.getXText());
		panel.add(BorderLayout.EAST, yLabel);
		panel.add(BorderLayout.EAST, map.getYText());
		panel.add(BorderLayout.EAST, floorLabel);
		panel.add(BorderLayout.EAST, map.getFloorText());

		panel.add(new JLabel("      "));

		panel.add(BorderLayout.EAST, new JLabel("Floor: "));
		String[] list3 = ClientConstants.getFloors();
		JComboBox combo3 = new JComboBox(list3);
		ActionListener combo3BoxActionListener = new ActionListener() {
		      public void actionPerformed(ActionEvent actionEvent) {
		    	  JComboBox aButton = (JComboBox) actionEvent.getSource();
		    	  map.setFloor(new Integer(aButton.getSelectedItem().toString()));
		      }
		};
		combo3.addActionListener(combo3BoxActionListener);
		panel.add(BorderLayout.EAST, combo3);	

		panel.add(new JLabel("      "));		
		
		//combobox
		panel.add(BorderLayout.EAST, new JLabel("Restriction: "));
		String[] list = ClientConstants.getUsers();
		JComboBox combo = new JComboBox(list);
		ActionListener comboBoxActionListener = new ActionListener() {
		      public void actionPerformed(ActionEvent actionEvent) {
		    	  JComboBox aButton = (JComboBox) actionEvent.getSource();
		    	  map.setRestriction(ClientConstants.getUserRestriction(aButton.getSelectedItem().toString()));
		      }
		};
		combo.addActionListener(comboBoxActionListener);
		panel.add(BorderLayout.EAST, combo);	

		panel.add(BorderLayout.EAST, new JLabel("      "));
		
		//combobox
		panel.add(BorderLayout.EAST, new JLabel("Language: "));
		String[] list2 = ClientConstants.getLanguages();
		JComboBox combo2 = new JComboBox(list2);
		ActionListener combo2BoxActionListener = new ActionListener() {
		      public void actionPerformed(ActionEvent actionEvent) {
		    	  JComboBox aButton = (JComboBox) actionEvent.getSource();
		    	  map.setLanguage(ClientConstants.getLanguage(aButton.getSelectedItem().toString()));
		      }
		};
		combo2.addActionListener(combo2BoxActionListener);
		panel.add(BorderLayout.EAST, combo2);	
		
		return panel;
	}
	
	private JPanel buildCommandsBar() {
		JPanel panel = new JPanel();
		
		// botoes simulacao e realtime
		panel.add(new JLabel("Type: "));
		ButtonGroup group = new ButtonGroup();
		JRadioButton radioRealTime = new JRadioButton(ClientConstants.REALTIME_LABEL, !map.isSimulation());
		JRadioButton radioSimulation = new JRadioButton(ClientConstants.SIMULATION_LABEL, map.isSimulation());		
		ActionListener radioButtonActionListener = new ActionListener() {
		      public void actionPerformed(ActionEvent actionEvent) {
		        AbstractButton aButton = (AbstractButton) actionEvent.getSource();
		        if (aButton.getText().compareTo(ClientConstants.REALTIME_LABEL) == 0) {
		        	map.setSimulation(false);
		        	map.resetSimulatedPositions();
		        	System.out.println("Não é simulação");
		        } else if (aButton.getText().compareTo(ClientConstants.SIMULATION_LABEL) == 0) {
		        	map.setSimulation(true);
		        	System.out.println("É simulação");
		        }
		      }
		};
		radioRealTime.addActionListener(radioButtonActionListener);
		radioSimulation.addActionListener(radioButtonActionListener);
		group.add(radioRealTime);
		group.add(radioSimulation);
		panel.add(radioRealTime);
		panel.add(radioSimulation);

		panel.add(new JLabel("        "));
		
		
		//combobox
		panel.add(new JLabel("Target: "));
		String[] list = ClientConstants.getTargets();
		JComboBox combo = new JComboBox(list);
		ActionListener comboBoxActionListener = new ActionListener() {
		      public void actionPerformed(ActionEvent actionEvent) {
		    	  JComboBox aButton = (JComboBox) actionEvent.getSource();
		    	  map.setTarget(ClientConstants.getTarget(aButton.getSelectedItem().toString()));
		      }
		};
		combo.addActionListener(comboBoxActionListener);
		panel.add(combo);	

		panel.add(new JLabel("        "));
		
		//iniciar
		JButton start = new JButton("Start");
		ActionListener startActionListener = new ActionListener() {
		      public void actionPerformed(ActionEvent actionEvent) {
		    	  map.start();
		      }
		};
		start.addActionListener(startActionListener);
		panel.add(start);

		panel.add(new JLabel("        "));
		
		//parar
		JButton stop = new JButton("Stop");
		ActionListener stopActionListener = new ActionListener() {
		      public void actionPerformed(ActionEvent actionEvent) {
		    	  map.stop();
		      }
		};
		stop.addActionListener(stopActionListener);
		panel.add(stop);

		panel.add(new JLabel("        "));
		
		//botoes zoom
		JCheckBox b1 = new JCheckBox("move");
		ActionListener b1ActionListener = new ActionListener() {
		      public void actionPerformed(ActionEvent actionEvent) {
		    	  map.getCanvas().alteraCentralizacaoAutomatica();
		      }
		};
		b1.addActionListener(b1ActionListener);
		panel.add(b1);

		panel.add(new JLabel("  "));

		JButton b2 = new JButton("+");
		ActionListener b2ActionListener = new ActionListener() {
		      public void actionPerformed(ActionEvent actionEvent) {
					map.getCanvas().ampliar();
		      }
		};
		b2.addActionListener(b2ActionListener);
		panel.add(b2);

		panel.add(new JLabel("  "));

		JButton b3 = new JButton("-");
		ActionListener b3ActionListener = new ActionListener() {
		      public void actionPerformed(ActionEvent actionEvent) {
					map.getCanvas().diminuir();
		      }
		};
		b3.addActionListener(b3ActionListener);
		panel.add(b3);

		panel.add(new JLabel(" "));
		
		return panel;
	}

}

class BarraCanvas extends Canvas implements ImageObserver, MouseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1408588148743329251L;

	Applet app;

	WAI2008ServiceMap mapa;
	
	Image img;

	public BarraCanvas(Applet _app, WAI2008ServiceMap _mapa, Image _img) {
		app = _app;
		mapa = _mapa;
		img = _img;
		img.flush();
		//setBounds(0, 0, 240, 25);
		setSize(83, 21);
		setBackground(Color.GRAY);
		addMouseListener(this);
	}

	public void mouseClicked(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		
		//System.out.println("posicao: ( " + x + ", " + y + ")");
		if (y>1 && y<20){
			if (x>=4 && x<=22){
				//System.out.println("mexer!!");
				mapa.getCanvas().alteraCentralizacaoAutomatica();
				repaint();
			}
			if (x>=27 && x<=46){
				//System.out.println("ampliar!!");
				mapa.getCanvas().ampliar();
			}
			if (x>=52 && x<=72){
				//System.out.println("diminuir!!");
				mapa.getCanvas().diminuir();
			}
			
		}
		e.consume();
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void paint(Graphics g) {
		g.drawImage(img, 0, 0, this);
		if (mapa.getCanvas().isCentralizacaoAutomatica()){
			g.setColor(Color.red);
			g.drawLine(5,2,21,19);
			g.drawLine(21,2,5,19);
		}
	}

}